package com.dk.controller;

import com.dk.Vo.TBookVo;
import com.dk.pojo.Publisher;
import com.dk.pojo.TAuthor;
import com.dk.pojo.TBook;
import com.dk.pojo.TCategory;
import com.dk.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("tBookController")
public class TBookController {

    //图书表
    @Autowired
    private TBookServiceI tBookServiceI;


    //出版社
    @Autowired
    private PublisherServiceI publisherServiceI;


    //作者
    @Autowired
    private TAuthorServiceI tAuthorServiceI;

    //类型
    @Autowired
    private TCategoryServiceI tCategoryServiceI;

    //跳转图书表页面
    @RequestMapping("list")
    public String listGet(Model model){

        //出版社
        List<Publisher> publisher =  publisherServiceI.getInfo();
        model.addAttribute("list",publisher);

        //分类
        List<TCategory> clist = tCategoryServiceI.getInfo();
        model.addAttribute("clist",clist);

        //作者
        List<TAuthor> tAuthors = tAuthorServiceI.getInfo();
        model.addAttribute("alist",tAuthors);
        return "bbook/list";
    }

    //查询图书页面
    @RequestMapping("getInfoServer")
    @ResponseBody
    public Map<String,Object> getInfoServer(@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="2")
            Integer pageSize,String bookName,Integer publisher,Integer author,Integer bookCategory){
            PageInfo<TBookVo> page = tBookServiceI.getInfoServer(pageNum,pageSize,bookName,publisher,author,bookCategory);
            Map<String,Object> map = new HashMap<>();
            map.put("total", page.getTotal());
            map.put("rows", page.getList());
        return map;
    }

    //增加图书
    @RequestMapping("insertInfo")
    public String insertInfo(Model model){

        //出版社
        List<Publisher> publisher =  publisherServiceI.getInfo();
        model.addAttribute("list",publisher);

        //分类
        List<TCategory> clist = tCategoryServiceI.getInfo();
        model.addAttribute("clist",clist);

        //作者
        List<TAuthor> tAuthors = tAuthorServiceI.getInfo();
        model.addAttribute("alist",tAuthors);

        return "bbook/add";
    }

    //确认增加
    @RequestMapping("addById")
    @ResponseBody
    public int addById(TBook tBook){
        tBookServiceI.addById(tBook);
        return 1;
    }

    //批量删除
    @RequestMapping("deleteBatch")
    @ResponseBody
    public int deleteBatch(@RequestParam("ids[]")Integer[] ids){
        tBookServiceI.deleteBatch(ids);
        return 1;
    }

    //回显
    @RequestMapping("updateInfo/{bookId}")
    public String updateInfo(@PathVariable("bookId")Integer bookId,Model model){

        //图书
        TBook tBook = tBookServiceI.getUpdateInfo(bookId);
        model.addAttribute("tBook",tBook);

        //出版社
        List<Publisher> publisher =  publisherServiceI.getInfo();
        model.addAttribute("list",publisher);

        //分类
        List<TCategory> clist = tCategoryServiceI.getInfo();
        model.addAttribute("clist",clist);

        //作者
        List<TAuthor> tAuthors = tAuthorServiceI.getInfo();
        model.addAttribute("alist",tAuthors);
        return "bbook/update";
    }

    //确认修改
    @RequestMapping("updateById")
    @ResponseBody
    public int updateById(TBook tBook){
        tBookServiceI.updateById(tBook);
        return 1;
    }

    //审核
    @RequestMapping("toList")
    public String ToGet(Model model){
        //出版社
        List<Publisher> publisher =  publisherServiceI.getInfo();
        model.addAttribute("list",publisher);

        //分类
        List<TCategory> clist = tCategoryServiceI.getInfo();
        model.addAttribute("clist",clist);

        //作者
        List<TAuthor> tAuthors = tAuthorServiceI.getInfo();
        model.addAttribute("alist",tAuthors);
        return "bbook/clist";
    }

    //审核查询
    @RequestMapping("getInfoServers")
    @ResponseBody
    public Map<String,Object> getInfoServers(@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="2")
            Integer pageSize,String bookName,Integer publisher,Integer author,Integer bookCategory){
            PageInfo<TBookVo> page = tBookServiceI.getInfoServers(pageNum,pageSize,bookName,publisher,author,bookCategory);
            Map<String,Object> map = new HashMap<>();
            map.put("total", page.getTotal());
            map.put("rows", page.getList());
             return map;
    }

    //修改审核
    @RequestMapping("upInfo/{bookId}")
    @ResponseBody
    public int upInfo(@PathVariable("bookId")Integer bookId){
        TBook tBook = tBookServiceI.getUpdateInfos(bookId);
        tBook.setBookState(0);
        tBookServiceI.updateState(tBook);
        return 1;
    }
}
